home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-08-01 | 5.5 KB | 245 lines |
- /*
- * BackCounter.java - A back counter
- * Copyright (C) 2000 Romain Guy
- * guy.romain@bigfoot.com
- * www.jext.org
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
- import waba.fx.*;
- import waba.ui.*;
-
- /**
- * Displays a back counter.
- * @author Romain Guy <guy.romain@bigfoot.com>
- * @version 1.1
- */
-
- public class BackCounter extends Control
- {
- ///////////////////////////////////////////////////////////////////////////////////////////////
- // CONSTANTS
- ///////////////////////////////////////////////////////////////////////////////////////////////
- // states
- public static final byte STOPPED = 0, STARTED = 1;
-
- ///////////////////////////////////////////////////////////////////////////////////////////////
- // PRIVATE FIELDS
- ///////////////////////////////////////////////////////////////////////////////////////////////
- // state
- public byte state = STOPPED;
- // the default number (-1) of minutes allowed
- public static final byte TIME = 30;
- // current minutes left
- private byte minutes = TIME;
- // current seconds left
- private byte seconds = 0;
- // parent
- private eCross parent;
- // graphics area
- private Graphics g;
- // the internal timer
- private Timer timer;
- // timer font
- private Font font;
- // timer label width and height
- private int timerWidth, timerHeight;
-
- /**
- * Creates a new timer linked to a specified <code>eCross</code>.
- * This timer counts backward and warn the eCross parent when time
- * is out.
- * @param parent The owner of the timer, which will be warned when time is out
- * @param x The x coordinate of the display
- * @param y The y coordinate of the display
- */
-
- public BackCounter(eCross parent, int x, int y)
- {
- this.parent = parent;
- g = parent.getBackBuffer();
- this.font = new Font("Helvetica", Font.BOLD, 10);
- FontMetrics fm = getFontMetrics(font);
- timerWidth = fm.getTextWidth("00:00") + 8;
- timerHeight = fm.getHeight() + 4;
-
- setRect(x, y, timerWidth, timerHeight);
- }
-
- ///////////////////////////////////////////////////////////////////////////////////////////////
- // PUBLIC METHODS
- ///////////////////////////////////////////////////////////////////////////////////////////////
-
- /**
- * Resolves time event.
- */
-
- public void onEvent(Event evt)
- {
- if (evt.type == ControlEvent.TIMER)
- {
- seconds--;
-
- if (seconds == -1)
- {
- seconds = 59;
- minutes--;
- if (minutes < 0)
- {
- stop();
- paint(true);
- parent.timeOut();
- return;
- }
- }
-
- paint(true);
- }
- }
-
- /**
- * Returns the current amount of minutes left.
- */
-
- public byte getMinutes()
- {
- return minutes;
- }
-
- /**
- * Returns the current amount of seconds left.
- */
-
- public byte getSeconds()
- {
- return seconds;
- }
-
- /**
- * Sets remaining time and starts.
- */
-
- public void setTime(byte minutes, byte seconds)
- {
- this.minutes = minutes;
- this.seconds = seconds;
- }
-
- /**
- * Removes a given amount of minutes to the current time.
- * @param minutes The amount of minutes to be removed
- */
-
- public void remove(byte minutes)
- {
- this.minutes -= minutes;
- if (this.minutes < 0)
- {
- stop();
- paint(true);
- parent.timeOut();
- }
- }
-
- /**
- * Starts the timer.
- * @param let If true, do not reset timer
- */
-
- public void start(boolean let)
- {
- if (!let)
- reset();
-
- timer = addTimer(1000);
- state = STARTED;
- }
-
- /**
- * Resumes the timer.
- */
-
- public void resume()
- {
- if (state == STARTED)
- timer = addTimer(1000);
- }
-
- /**
- * Pauses the timer.
- */
-
- public void pause()
- {
- if (state == STARTED)
- removeTimer(timer);
- }
-
- /**
- * Stops the timer.
- */
-
- public void stop()
- {
- removeTimer(timer);
- minutes = 0;
- seconds = 0;
- state = STOPPED;
- }
-
- /**
- * Resets the time left.
- */
-
- public void reset()
- {
- minutes = TIME;
- seconds = 0;
- state = STOPPED;
- }
-
- /**
- * Paints the timer on screen.
- * @param direct If <code>true</code>, the timer is also painted on screen
- * without waiting for <code>eCross</code> to repaint it entirely
- */
-
- public void paint(boolean direct)
- {
- g.setColor(255, 255, 255);
- g.fillRect(x + 1, y + 1, timerWidth - 1, timerHeight - 1);
- g.setColor(0, 0, 0);
- g.drawRect(x, y, timerWidth, timerHeight);
-
- g.setFont(font);
- StringBuffer buf = new StringBuffer();
-
- if (minutes < 10)
- buf.append('0');
- buf.append(minutes).append(':');
-
- if (seconds < 10)
- buf.append('0');
-
- g.drawText(buf.append(seconds).toString(), x + 4, y + 2);
-
- if (direct)
- parent.drawTimer(timerWidth, timerHeight);
- }
- }
-
- // End of BackCounter.java
-